require "import"
import "tools"
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS).setStatusBarColor(0xff4DB6AC)
activity.setContentView("aTransform_")
format={"aac","mp2","ts","m4a","ogg","wma","wav","flac","ac3","pcm","aiff","tta","sbc","eac3","aptx"}
adp=ArrayAdapter(activity,android.R.layout.simple_spinner_item, String(format))
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
transform.background=btBackground
sp.Adapter=adp
Define.onClick=function()
  Default.checked=false
  setEnable(true)
end
Default.onClick=function()
  Define.checked=false
  setEnable(false)
end
function setEnable(a)
  start.setEnabled(a)
  End.setEnabled(a)
  bitrate.setEnabled(a)
  frames.setEnabled(a)
  channel.setEnabled(a)
  volume.setEnabled(a)
  sample.setEnabled(a)
  aq.setEnabled(a)
end
Default.checked=true
setEnable(false)
oDir.text=LUA_SD.."/avHandle/output/"
iDir.onClick=function()
  ChoiceFile(LUA_SD,function(dir)
    iDir.text=dir
    dialogPlayer(dir)
  end)
end
oDir.onClick=function()
  ChoicePath(LUA_SD,function(dir)
    oDir.text=dir.."/"
  end)
end
transform.onClick=function()
  if File(iDir.text).exists()
    if filename.text~=""
      local cmd="-i "..iDir.text.." -y "
      if Define.checked
        if start.text~=""
          cmd = cmd.."-ss "..start.text.." "
        end
        if End.text~=""
          cmd = cmd.."-t "..End.text.." "
        end
        if bitrate.text~=""
          cmd = cmd.."-ab "..bitrate.text.."k "
        end
        if channel.text~=""
          cmd = cmd.."-ac "..channel.text.." "
        end
        if sample.text~=""
          cmd = cmd.."-ar "..sample.text.." "
        end
        if volume.text~=""
          cmd = cmd.."-vol "..volume.text.." "
        end
        if frames.text~=""
          cmd = cmd.."-aframes "..frames.text.." "
        end
        if aq.text~=""
          cmd = cmd.."-aq "..aq.text.." "
        end
      end
      if tostring(sp.getSelectedItem())=="pcm"
        cmd=cmd.."-f s16le "
      end
      local s=tostring(File(iDir.text).getName())
      s=s:sub(s:find("%."),#s)
      if s==".mp3"--mp3不能直接转m4a和ogg，因为容器格式不一样，要先转aac数据
        if tostring(sp.getSelectedItem())=="m4a" or tostring(sp.getSelectedItem())=="ogg"
          cmd = cmd..LUA_SD.."/avHandle/tmp/"..filename.text..".aac"
          local cmd2="-i "..LUA_SD.."/avHandle/tmp/"..filename.text..".aac -y "..oDir.text..filename.text.."."..sp.getSelectedItem()
          activity.newActivity("handle",{cmd,cmd2})
          return
        end
      end
      cmd = cmd..oDir.text..filename.text.."."..sp.getSelectedItem()
      activity.newActivity("handle",{cmd})
     else
      print("请输入文件名")
    end
   else
    print("目标文件不存在")
  end
end
